<template>
  <div>
    <h2>理财添加页面</h2>
    <table class="table" border="1">
      <tr>
        <td>理财Id</td>
        <td>
          <input type="text" v-model="state.F_id">
        </td>
      </tr>
      <tr>
        <td>理财名称</td>
        <td>
          <input type="text" v-model="state.Fname">
        </td>
      </tr>
      <tr>
        <td>年收益</td>
        <td>
          <input type="text" v-model="state.Year_sum">
        </td>
      </tr>
      <tr>
        <td>利息</td>
        <td>
          <select v-model="state.FL_id">
            <option value="">请选择</option>
            <option v-for="item in list" :value="item.FL_id">每年{{item.FL_num}}%</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>最低金额</td>
        <td>
          <input type="text" v-model="state.Min_money">
        </td>
      </tr>
      <tr>
        <td>风险等级</td>
        <td>
          <select v-model="state.Lv_id">
            <option value="">请选择</option>
            <option v-for="item in list1" :value="item.Lv_id">{{item.LvName}}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2"><input type="button" value="添加" @click="add"></td>
        <td></td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">
import router from '@/router';
import axios from 'axios';
import {reactive,toRefs,onMounted} from 'vue'
let data=reactive({
  state:{
    F_id:"",
    Fname:"",
    Year_sum:"",
    FL_id:"",
    Min_money:"",
    Lv_id:"",
    F_Num:0
  },
  list:{},
  list1:{}
})
let {state,list,list1}=toRefs(data)
onMounted(()=>{
  getlist()
  getlist1();
})
//绑定利息
function getlist()
{
  axios({
    url:"http://localhost:51322/api/Fin/GetFinLixiList",
    method:"get",
  })
  .then(res=>{
    console.log(res);
    list.value=res.data.data
  })
  .catch(err=>{
    console.log(err);
  })
}
//绑定风险等级
function getlist1()
{
  axios({
    url:"http://localhost:51322/api/Fin/GetFinLvList",
    method:"get",
  })
  .then(res=>{
    console.log(res);
    list1.value=res.data.data
  })
  .catch(err=>{
    console.log(err);
  })
}
function add()
{
  if(state.value.F_id=="")
{
  alert("理财ID不能为空");
  return;
}
if(Number(state.value.Min_money)<=0)
{
  alert("最低金额必须大于0");
  return
}
  axios({
    url:"http://localhost:51322/api/Fin/PostFinAdd",
    method:"post",
    data:state.value
  })
  .then(res=>{
    console.log(res);
    if(res.data.data>0)
  {
    alert("理财添加成功");
    router.push("/FinList")
  }
  else if(res.data.data==-1)
  {
    alert("理财项目已存在");
    return;
  }
  else{
    alert("添加失败");
    return;
  }
  })
  .catch(err=>{
    console.log(err);
  })
}
</script>

<style scoped>

</style>